home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / CmdLine / src / CmdLine.C.bak < prev    next >
Text File  |  1991-06-14  |  4KB  |  164 lines

  1. #include "CmdLine.h"
  2.  
  3. #include <stdlib.h>
  4. //#include <string.h>
  5. #include <osfcn.h>
  6. #include <iostream.h>
  7.  
  8. CmdLine::CmdLine(RJS_CmdOpt **opt, const char *p,    CmdLineFlags f)
  9. {
  10.  
  11.     prefix=p;
  12.     flags=f;
  13.     options=opt;
  14.     if (abbrev()) setup_abbrevs();
  15.         else for (int i=0; options[i] != NULL; i++)
  16.         options[i]->abbrev_len = options[i]->keyword().length();
  17.  
  18.     if (dcl()) sep='/'; else sep='-';
  19. }
  20.  
  21. void CmdLine::setup_abbrevs()
  22. {
  23.     int i,j,l,min_len;
  24.  
  25.     for (i=0; options[i] != NULL; i++) {
  26.       options[i]->abbrev_len=1;
  27.       for (j=0; options[j] != NULL; j++) if (i==j) continue; else {
  28.         min_len= (options[i]->keyword().length() <= options[j]->keyword().length()) ?
  29.             options[i]->keyword().length() : options[j]->keyword().length();
  30.         for (l=0; l<min_len; l++) 
  31.            if (options[i]->keyword()[l] != options[j]->keyword()[l]) break;
  32.         l++;
  33.         if (l > options[i]->abbrev_len) {
  34.            if (l < options[i]->keyword().length())
  35.             options[i]->abbrev_len=l;
  36.            else
  37.             options[i]->abbrev_len=options[i]->keyword().length();
  38.         }
  39.       }
  40.         }
  41. }
  42.  
  43. CmdLine::ParseStatus CmdLine::parse(char **argv, String &xtra)
  44. {
  45.     CmdLineWord clw(argv);
  46.     return parse(clw,xtra);
  47. }
  48.  
  49. CmdLine::ParseStatus CmdLine::parse(String &cmdline, String &xtra)
  50. {
  51.     CmdLineWord clw(cmdline,dcl());
  52.     return parse(clw,xtra);
  53. }
  54.  
  55. CmdLine::ParseStatus CmdLine::parse(CmdLineWord &clw, String &xtra)
  56. {
  57.  
  58.  xtra="";
  59.  int i;
  60.  int no_more_options=0;
  61.  
  62.  for (i=0; options[i] != NULL; i++) options[i]-> reset();
  63.  
  64.  while (clw(opt)) {
  65.  
  66.      if (opt.contains('=')) {
  67.     val=opt.after('=');
  68.     opt=opt.before('=');
  69.      } else val="";
  70.  
  71.      if (opt[0]!=sep || opt=='-' || no_more_options) {
  72.         // parameter checking here!
  73.         xtra += (opt + ' ');
  74.         continue;
  75.      } else if ( opt.length()>1 && opt[0]=='-' && opt[1]=='-') {
  76.     no_more_options=1;
  77.      continue;
  78.      }
  79.  
  80.      String copt = opt(1); // remove separtor ('-'|'/')
  81.  
  82.      for (i=0; options[i] != NULL; i++) {
  83.       if (copt.length() > options[i]->keyword().length() || 
  84.             copt.length() < options[i]->abbrev_len) continue;
  85.       if (no_case()) {
  86.           if (options[i]->keyword().substr(0,copt.length())==copt.icase())
  87.                                      break;
  88.           }
  89.       else if (options[i]->keyword().substr(0,copt.length())==copt) break;
  90.      }
  91.  
  92.      if (options[i]==NULL) {
  93.     if (messages())
  94.         cerr << prefix << ": error unknown option: "<< opt << endl;
  95.     if (abort()) exit(1); else return UnknownOption;
  96.      }
  97.  
  98.      if (options[i]->is_present() && only_once()) {
  99.     if (messages())
  100.         cerr << prefix << ": option specified more then once:" <<sep<< opt << endl;
  101.     if (abort()) exit(1); else return MoreThenOnce;
  102.      }
  103.  
  104.      if (options[i]->expects_value()) {        // option has a required valu
  105.     if (val=="") {
  106.         if (options[i]->value_optional()) {
  107.             String temp;
  108.             if(clw.peek(temp) && (temp[0]!=sep || temp=="--")) 
  109.                 clw(val);
  110.             else {
  111.                 options[i]->set();
  112.                 continue; // get next option
  113.             }
  114.         } else clw(val);
  115.     }
  116.        if (!val) {
  117.         if (messages())
  118.           cerr << prefix << ": missing " << options[i]->value_type() 
  119.            << " arguement for " <<sep<< options[i]->keyword() << endl; 
  120.         if (abort()) exit(1); else return NoValue;
  121.     } else {
  122.        if (!options[i]->set(val)) {
  123.         if (messages()) 
  124.              cerr << prefix << ": invalid arguement for "<<sep << 
  125.                                options[i]->keyword() << ", expecting "<<
  126.                           options[i]->value_type() << endl; 
  127.         if (abort()) exit(1); else return BadValue;
  128.        } 
  129.     }
  130.      } else options[i]->set();
  131.   } // end of while
  132.  
  133.  
  134.  for (i=0; options[i] != NULL; i++) {
  135.     if (options[i]->is_required() && !options[i]->is_present()) {
  136.     if (messages())
  137.         cerr << prefix << ": "<<sep << options[i]->keyword() <<
  138.             " is required" << endl; 
  139.     if (abort()) exit(1); else return MissingRequired;
  140.     }
  141.     if (options[i]->has_default() && !options[i]->is_present()) {
  142.        if (!options[i]->set_default()) {
  143.         if (messages()) 
  144.              cerr << prefix << ": invalid arguement for "<<sep << 
  145.                                options[i]->keyword() << ", expecting "<<
  146.                           options[i]->value_type() << endl; 
  147.         if (abort()) exit(1); else return BadValue;
  148.        }
  149.     }
  150.  
  151.  }
  152.  
  153.  return Ok;
  154.  
  155. }
  156.  
  157. void CmdLine::dump()
  158. {
  159.      for (int i=0; options[i] != NULL; i++) options[i]->dump();
  160. }
  161.  
  162.  
  163.  
  164.